#include <QtGui>
#include <QSettings>
#include <QDir>
#include "Config.h"
#include "EventBus.h"
#include "Emboard.h"
#include "Viboard.h"
#include "FakeJtag.h"

using namespace etb;

int main(int argc, char **argv)
{
  QApplication app(argc, argv);

  QSettings settings(QDir::currentPath() + QDir::separator() + ETB_CONF_FILE, 
                     QSettings::IniFormat);
  settings.beginGroup("Board");
  bool bootMode = settings.value("boot_mode", true).toBool();
  settings.endGroup();

  QPoint defaultPos(200, 200);
  settings.beginGroup("Viboard");
  QPoint vp = settings.value("viboard_pos", defaultPos).toPoint();
  QPoint up = settings.value("uart_pos", defaultPos).toPoint();
  settings.endGroup();

  settings.beginGroup("FakeJtag");
  QPoint fp = settings.value("pos", defaultPos).toPoint();
  settings.endGroup();

  EventBus *bus = new EventBus();
  Emboard *eb = new Emboard(bus);
  FakeJtag *fj = new FakeJtag();
  Viboard *vb = new Viboard(bus, fj, NULL);
  fj->setEmboard(eb);
  fj->move(fp);

  eb->init(bootMode);
  vb->init(bootMode);
  bus->start();

  vb->setWindowTitle("ETB Simulator(v" ETB_VERSION ")");
  vb->setJtag(fj);

  vb->move(vp);
  vb->setUartWindowPos(up);
  vb->show();

  app.exec();

  settings.beginGroup("Board");
  settings.setValue("boot_mode", vb->getBootMode());
  settings.endGroup();
  settings.beginGroup("Viboard");
  settings.setValue("viboard_pos", vb->pos());
  settings.setValue("uart_pos", vb->getUartWindowPos());
  settings.endGroup();
  settings.beginGroup("FakeJtag");
  settings.setValue("pos", fj->pos());
  settings.endGroup();

  bus->stop();

  delete bus;
  delete eb;
  delete fj;

  return 0;
}
